package com.mg.admin.controller.admin;

import com.mg.admin.bean.admin.DictVO;
import com.mg.admin.bean.admin.Result;
import com.mg.admin.bean.admin.WxMenuVO;
import com.mg.admin.bean.admin.WxSetVO;
import com.mg.admin.controller.BaseController;
import com.mg.admin.service.admin.DictService;
import com.mg.admin.service.admin.WxMenuService;
import com.mg.admin.service.admin.WxSetService;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.mg.admin.controller.BaseController.LIST;
import static com.mg.admin.controller.BaseController.PAGE;

/**
 * 公众号自定义菜单管理
 * Created by jujunchen on 2017/9/24.
 */
@Controller
@RequestMapping("/admin/wx/menu/")
public class WxMenuController extends BaseController {

    @Autowired
    private WxMenuService wxMenuService;

    @Autowired
    private DictService dictService;

    @Autowired
    private WxMpService wxMpService;

    //数据字典，微信菜单位置分类id
    private static  final Long DICT_WX_MENU_ID = 22l;

    /**
     * 菜单列表
     */
    @GetMapping(LIST)
    public void list(ModelMap modelMap){
        List<DictVO> list = dictService.findByClassId(DICT_WX_MENU_ID);
        modelMap.put("ordersList",list);
    }

    @ResponseBody
    @PostMapping(PAGE)
    public Result<List<WxMenuVO>> page(){
        List<WxMenuVO> list = wxMenuService.findAll(null);
        return  new Result<>(list);
    }

    @GetMapping(ADD)
    public void add(ModelMap modelMap){
        List<DictVO> list = dictService.findByClassId(DICT_WX_MENU_ID);
        modelMap.put("ordersList",list);
    }

    @GetMapping(EDIT)
    public void edit(ModelMap modelMap,Long id){
        List<DictVO> list = dictService.findByClassId(DICT_WX_MENU_ID);
        modelMap.put("ordersList",list);
        WxMenuVO wxMenuVO = wxMenuService.findOneById(id);
        modelMap.put("bean",wxMenuVO);
    }

    @ResponseBody
    @PostMapping(SAVE)
    public Result save(WxMenuVO wxMenuVO){
        if (wxMenuVO.getId() ==null){
            wxMenuService.insertOne(wxMenuVO);
        }else {
            wxMenuService.updateOne(wxMenuVO);
        }
        return new Result();
    }

    @GetMapping(DELETE)
    @ResponseBody
    public void delete(Long id){
        wxMenuService.delete(id);
    }

    @ResponseBody
    @GetMapping("sync")
    public void sync(){
        List<WxMenuVO> list = wxMenuService.findAll(1);
        WxMenu menu = new WxMenu();
        List<WxMenuButton> topButtonList = new ArrayList<>();
        for (WxMenuVO vo : list){
            WxMenuButton button = new WxMenuButton();

            if (vo.getType().equals(WxConsts.BUTTON_CLICK)){
                button.setType(WxConsts.BUTTON_CLICK);
                button.setName(vo.getName());
                button.setKey(vo.getKeyCode());
            }else {
                button.setType(WxConsts.BUTTON_VIEW);
                button.setName(vo.getName());
                button.setUrl(vo.getUrl());
            }

            if (vo.getOrders().length() ==1){
                topButtonList.add(button);
                menu.getButtons().add(button);
            }else {
                int leftOrder = Integer.parseInt(vo.getOrders().substring(0,1));
                WxMenuButton topButton = topButtonList.get(leftOrder - 1);
                topButton.getSubButtons().add(button);
            }
        }
        try {
            wxMpService.getMenuService().menuCreate(menu);
        } catch (WxErrorException e) {
            logger.error(e.getMessage());
        }
    }


    /**
     * bootstrap树形表格，没有采用，以后用到再用
     * @return
     */

    @ResponseBody
    @GetMapping("distlistjson")
    public Map<String,Object> distlistjson(){
        List<Map<String,Object>> list  = new ArrayList<>();

        Map<String,Object> map = new HashMap<>();
        map.put("Id",1);
        map.put("Name","系统设置");
        map.put("Url",null);
        map.put("ParentId",null);
        map.put("Level",1);

        Map<String,Object> map1 = new HashMap<>();
        map1.put("Id",2);
        map1.put("Name","菜单管理");
        map1.put("Url",null);
        map1.put("ParentId",1);
        map1.put("Level",2);
        list.add(map);
        list.add(map1);

        Map<String,Object> reMap = new HashMap<>();
        reMap.put("rows",list);
        reMap.put("total",2);

        return reMap;

    }


    @GetMapping("distlist")
    public void distlist(){}



}
